package com.guns.utils;

import com.guns.JNADll;

import java.sql.*;
import java.text.SimpleDateFormat;

public class JdbcUse {
    public static String findCar="select * from sys_car_info where plate_number=?";
    public static String addActionLog="insert into car_action_log(car_number,io_time,channel_id,direction,log_type,remark) values(?,?,?,?,?,?) ";
    public static String findTable="select * from sys_car where number=? and (status=3 or status=1)";
    public static String updateCar="update sys_car_info set status=? where plate_number=?";
    public static String updateTable="update sys_car set status=? where number=?";
    public static String updateCarMa="update sys_car_ma set status=? where plate_numbr=?";

    //连接数据库
    public Connection connect(String user,String pwd) {
        try {
            Connection con = null;
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/guns?useUnicode=true&characterEncoding=UTF-8",user,pwd);
            return con;
        }
        catch (Exception e){
            System.out.println("MYSQL error" + e.getMessage());
        }
        return null;
    }

    //查询车辆
    public int carFind(Connection con,String carNumber,String ioTime,String channelId,int direction){
        try {
            PreparedStatement ps = con.prepareStatement(findCar);
            ps.setString(1,carNumber);
            ResultSet rs = ps.executeQuery();
            //外来车辆
            if(!rs.next()){
            logSave(con,carNumber,ioTime,channelId,"入场","异常通行记录","外来车辆异常记录");
            return 0;
            }

            //注意车的类型！！
            else{
                int type=rs.getInt("type");
                System.out.println("sssssssss"+rs.getString("type"));
                if(type==1){
                    if(direction==0){
                        updateSql(con,updateCar,carNumber,0);
                        updateSql(con,updateCarMa,carNumber,1);
                        logSave(con,carNumber,ioTime,channelId,"入场","正常通行记录","");
                        return 1;
                    }
                    else {
                        updateSql(con,updateCar,carNumber,1);
                        updateSql(con,updateCarMa,carNumber,0);
                        logSave(con,carNumber,ioTime,channelId,"出场","正常通行记录","");
                        return 1;
                    }
                }
                else{
                return tableFind(con,carNumber,ioTime);
                }
            }
        }
        catch (Exception e){

        }
         return 6;
    }
    //添加日志
    public void logSave(Connection con,String carNumber,String ioTime,String channelId,String direction,String logType,String remark){
        try {
            PreparedStatement ps = con.prepareStatement(addActionLog);
            ps.setString(1,carNumber);
            ps.setString(2,ioTime);
            ps.setString(3,channelId);
            ps.setString(4,direction);
            ps.setString(5,logType);
            ps.setString(6,remark);
            int status=ps.executeUpdate();
            if(status>0){
                System.out.println("添加成功");
            }


        }
        catch (Exception e){
            System.out.println("添加失败");
        }
    }

    //查询派遣单
    public int tableFind(Connection con,String carNumber,String ioTime){
        try {
            PreparedStatement ps = con.prepareStatement(findTable);
            ps.setString(1,carNumber);
            ResultSet rs = ps.executeQuery();
           if(!rs.next()) {
               logSave(con,carNumber,ioTime,"1","出场","异常通行记录","没有预约就要出场");
               return 2;
            }
            else{
               //已预约要出场
                if(rs.getInt("status")==3){
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Timestamp timestamp = rs.getTimestamp("cometime");
                    String goTime=timestamp.toLocaleString();
                    if(format.parse(goTime).getTime()>format.parse(ioTime).getTime()){
                        logSave(con,carNumber,ioTime,"1","出场","异常通行记录","没有到出场时间就要出场");
                        return 3;
                    }
                    else {
                        logSave(con,carNumber,ioTime,"1","出场","正常通行记录","");
                        updateSql(con,updateTable,carNumber,1);
                        updateSql(con,updateCar,carNumber,1);
                        updateSql(con,updateCarMa,carNumber,0);
                        return 4;

                    }

                }
                //在途中返场
                else{
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Timestamp timestamp = rs.getTimestamp("gotime");
                    String comeTime=timestamp.toLocaleString();
                    if(format.parse(comeTime).getTime()>format.parse(ioTime).getTime()){
                        logSave(con,carNumber,ioTime,"1","入场","正常通行记录","");
                        updateSql(con,updateTable,carNumber,0);
                        updateSql(con,updateCar,carNumber,0);
                        updateSql(con,updateCarMa,carNumber,1);
                        return 4;
                    }
                    else{
                        logSave(con,carNumber,ioTime,"1","入场","异常通行记录","没有在:"+comeTime+" 时间内返场");
                        updateSql(con,updateTable,carNumber,0);
                        updateSql(con,updateCar,carNumber,0);
                        updateSql(con,updateCarMa,carNumber,1);
                        return 5;
                    }
                }


           }
        }
        catch (Exception e){

        }
      return 6;
    }
    public void updateSql(Connection con,String sql,String carNumber,int status){
        try {
            PreparedStatement sta = con.prepareStatement(sql);
            sta.setInt(1,status );
            sta.setString(2,carNumber );
            int result=sta.executeUpdate();
            if(result>0){
                System.out.println("修改成功");
            }
        }
        catch (Exception e){

        }
    }
}
